package com.yanqu.road.server.cmd.vehicle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionwar.UserVehicleSendTimes;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.player.UserVehicle;
import com.yanqu.road.pb.activity.RankUnionWarProto;
import com.yanqu.road.pb.vehicle.VehicleProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.vehicle.VehicleModule;
import com.yanqu.road.server.manger.activity.rankunionwar.RankUnionWarMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionPhaseWarMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWar;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.S_VEHICLE_ACTIVITY_WAR_TIMES, desc = "珍兽的活动出战次数")
public class VehicleActivityWarTimesCmd extends AbstractCommand {

    private int activityId;
    private int activityType;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        VehicleProto.VehicleActivityWarTimesReqMsg reqMsg = VehicleProto.VehicleActivityWarTimesReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        activityType = reqMsg.getActivityType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(eActivityType.CrossUnionWarActivity.getValue() == activityType){
            ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
            if(activityInfo == null){
                return;
            }
        }else if(eActivityType.RankUnionWar.getValue() == activityType){
            if(RankUnionWarMgr.getActivityId() != activityId){
                return;
            }
        }else {
            return;
        }

        Map<String, UserVehicle> userVehicleMap = player.getModule(VehicleModule.class).getUserVehicleMap();
        List<UserVehicle> userVehicleList = new ArrayList<>(userVehicleMap.values());
        if(eActivityType.CrossUnionWarActivity.getValue() == activityType){
            Map<String, UserVehicleSendTimes> userVehicleSendTimesMap = null;
            UnionWar unionWar = UnionPhaseWarMgr.getUnionWar(activityId);
            if(unionWar != null){
                if(unionWar.getUserVehicleSendMap().containsKey(player.getUserId())){
                    userVehicleSendTimesMap = unionWar.getUserVehicleSendMap().get(player.getUserId());
                }
            }
            VehicleProto.VehicleActivityWarTimesRespMsg.Builder respMsg = VehicleProto.VehicleActivityWarTimesRespMsg.newBuilder();
            for(UserVehicle userVehicle : userVehicleList){
                VehicleProto.VehicleActivityWarTimesTempMsg.Builder temp = VehicleProto.VehicleActivityWarTimesTempMsg.newBuilder();
                temp.setVehicleUid(userVehicle.getVehicleUid());
                if(userVehicleSendTimesMap != null && userVehicleSendTimesMap.containsKey(userVehicle.getVehicleUid())) {
                    UserVehicleSendTimes sendTimes = userVehicleSendTimesMap.get(userVehicle.getVehicleUid());
                    temp.setWarTimes(sendTimes.getTimes());
                }else{
                    temp.setWarTimes(0);
                }
                respMsg.addTimes(temp);
            }
            respMsg.setRet(0);
            respMsg.setActivityId(activityId);
            respMsg.setActivityType(activityType);
            player.sendPacket(Protocol.U_VEHICLE_ACTIVITY_WAR_TIMES, respMsg);
        }else if(eActivityType.RankUnionWar.getValue() == activityType){
            RankUnionWarProto.CrossVehicleActivityWarTimesReqMsg.Builder reqMsg = RankUnionWarProto.CrossVehicleActivityWarTimesReqMsg.newBuilder();
            for(UserVehicle userVehicle : userVehicleList){
                VehicleProto.VehicleActivityWarTimesTempMsg.Builder temp = VehicleProto.VehicleActivityWarTimesTempMsg.newBuilder();
                temp.setVehicleUid(userVehicle.getVehicleUid());
                temp.setWarTimes(0);
                reqMsg.addTimes(temp);
            }
            reqMsg.setActivityId(activityId);
            reqMsg.setActivityType(activityType);
            player.sendPacket(CrossProtocol.C2_CROSS_RANKUNIONWAR_VEHICLE_TIMES, reqMsg);

        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
